<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class MailPage {
	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("READ");
		if (!$_SESSION['user_id'])
			errorpage(6);
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {

		if ($_GET['out']) // outbox
			$sql = (db_query_select(array(array(), "MAIL_HEADER.*, MAIL_BODY.*, '{$_SESSION['member']['nick']}' AS sender_nick"), array("(((MAIL_HEADER INNER JOIN MAIL_BODY ON MAIL_HEADER.mail_id = MAIL_BODY.mail_id)) INNER JOIN MEMBER AS M1 ON M1.user_id = MAIL_HEADER.sender)", "DUMMY"), "sender = {$_SESSION['user_id']} AND !MAIL_HEADER.deleted AND MAIL_HEADER.mail_id = {$page->id}"));
		else // inbox
			$sql = (db_query_select(array(array(), "MAIL_HEADER.*, MAIL_BODY.*, MAIL_TO.user_id, MAIL_TO.hasread AS hasread_to, M1.nick AS sender_nick"), array("(((MAIL_HEADER INNER JOIN MAIL_BODY ON MAIL_HEADER.mail_id = MAIL_BODY.mail_id) INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER AS M1 ON M1.user_id = MAIL_HEADER.sender)", "DUMMY"), "MAIL_TO.user_id = {$_SESSION['user_id']} AND !MAIL_TO.deleted AND MAIL_HEADER.mail_id = {$page->id}"));
		
		$sqlresult = db_do_query($page, $sql);
		if (db_num_rows($sqlresult) != 1)
			errorpage(10);
		
		$mail = db_fetch_array($sqlresult);

		// mark mail as read
		if ($_GET['out'] and !$mail['hasread'] and $_SESSION['member']['pref_auto_mark_read'])
			db_do_query($page, db_query_update(array('sent', 'hasread'), array('sent', 1), "MAIL_HEADER", "mail_id = {$page->id}"));
		if (!$_GET['out'] and !$mail['hasread_to'] and $_SESSION['member']['pref_auto_mark_read'])
			db_do_query($page, db_query_update(array('hasread'), array(1), "MAIL_TO", "mail_id = {$page->id} AND user_id = {$_SESSION['user_id']}"));
			
		
		// mailbox browsing actions
		if (!$_GET['out'] and !$_GET['trash']) { // inbox
			$CONTENT['title'] = "INBOX: {$_SESSION['member']['nick']}";
		} else if (!$_GET['out'] and $_GET['trash']) { // inbox > trash
			$CONTENT['title'] = "INBOX [prullenbak]: {$_SESSION['member']['nick']}";
		} else if ($_GET['out'] and !$_GET['trash']) { // outbox
			$CONTENT['title'] = "OUTBOX: {$_SESSION['member']['nick']}";
		} else if ($_GET['out'] and $_GET['trash']) { // outbox > trashed
			$CONTENT['title'] = "OUTBOX [prullenbak]: {$_SESSION['member']['nick']}";
		}

		add_action('inbox', 'url', '.mailbox');
		add_action('outbox', 'url', '.mailbox?out=1');
		add_action('inbox-t', 'url', '.mailbox?trash=1');
		add_action('outbox-t', 'url', '.mailbox?out=1&trash=1');
		
		// mail actions	
		add_action('mail-reply', 'javascript', '', 'mail_reply();', 1);
		add_action('mail-replyall', 'javascript', '', 'mail_reply_all();', 1);
		add_action('mail-forward', 'javascript', '', 'mail_forward();', 1);
		add_action('mail-unread', 'javascript', '', 'document.unreadform.submit()');
		if ($_GET['trash'])
			add_action('mail-untrash', 'javascript', '', 'document.untrashform.submit()');
		else
			add_action('mail-trash', 'javascript', '', 'mail_trash();');
		add_action('mail-delete', 'javascript', '', 'mail_delete();');

		// retrieve adressees
		$sqlresult = db_do_query($page, db_query_select(array(array(), "MEMBER.nick, MAIL_TO.cc, MAIL_TO.user_id"), array("MAIL_TO INNER JOIN MEMBER ON MAIL_TO.user_id = MEMBER.user_id", "DUMMY"), "mail_id = {$page->id}", "cc ASC"));
		$mail['to'] = array();
		$mail['cc'] = array();
		while ($to = db_fetch_array($sqlresult))
			if ($to['cc'])
				$mail['cc'][] = array('user_id' => $to['user_id'], 'nick' => $to['nick']);
			else
				$mail['to'][] = array('user_id' => $to['user_id'], 'nick' => $to['nick']);

		$page->content['mail'] = $mail;
		$page->content['title'] = "Bericht #{$mail['mail_id']}";
	}


	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_mail($page->content['mail'], $_GET['out'], $_GET['trash']);
		print_form($page, $page->content['deleteform']);
	}
}

?>